package com.jie.demo01;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

//练习Thread,实现多线程同步下载图片
public class TestThread2 implements Runnable{
    private String url;  // 网络图片地址
    private String name; //保存的文件名

    public TestThread2(String url,String name){
        this.url=url;
        this.name=name;
    }
    @Override
    public void run() {
        WebDownloader webDownloader = new WebDownloader();
        webDownloader.downloader(url,name);
        System.out.println("下载了名为："+name);
    }

    public static void main(String[] args) {
        TestThread2 s1 = new TestThread2("https://tse2-mm.cn.bing.net/th/id/OIP-C.tJw04gKf3s6K8E9RhzNLLwHaJ0?pid=ImgDet&rs=1.jpg","漩涡鸣人.jpg");
        TestThread2 s2 = new TestThread2("https://gss0.baidu.com/-Po3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/e4dde71190ef76c62ca8339c9e16fdfaaf51670b.jpg","宇智波佐助.jpg");
        TestThread2 s3 = new TestThread2("https://pic.baike.soso.com/p/20120617/20120617115620-1648625124.jpg","卡卡西.jpg");
        new Thread(s1).start();
        new Thread(s2).start();
        new Thread(s3).start();

    }
}


//下载器
class WebDownloader {


    //下载方法
    public void downloader(String url,String name){
        try {
            FileUtils.copyURLToFile(new URL(url),new File(name));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("IO异常，download方法出现问题");
        }
    }
}